﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using webapp4U.BOL;
using webapp4U;
/// <summary>
/// Summary description for Products
/// </summary>
namespace webapp4U.DAL
{
    public class ProductsDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new Products instance filled with the DataReader's current record data 
        /// </summary>
        private Products GetProductsFromReader(IDataReader dr)
        {
            Products obj = new Products();
            //obj.ProductID = (dr["ProductID"] is DBNull) ? Int32.MinValue : (Int32)dr["ProductID"];
            //obj.AddedDate = (dr["AddedDate"] is DBNull) ? DateTime.MinValue : (DateTime)dr["AddedDate"];
            //obj.AddedBy = (dr["AddedBy"] is DBNull) ? String.Empty : (String)dr["AddedBy"];
            obj.InClassKey = (dr["inclasskey"] is DBNull) ? String.Empty : (String)dr["inclasskey"];
            obj.ItemKey = (dr["itemkey"] is DBNull) ? String.Empty : (String)dr["itemkey"];
            obj.Description = (dr["description"] is DBNull) ? String.Empty : (String)dr["description"];            
            obj.UnitPrice = (dr["price"] is DBNull) ? float.MinValue : float.Parse(dr["price"].ToString());
            obj.ImageUrl = (dr["ImgName"] is DBNull) ? String.Empty : (String)dr["ImgName"];   
            return obj;
        }
        #endregion

        private List<Products> GetProductsCollectionFromReader(IDataReader dr)
        {
            
            List<Products> obj = new List<Products>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    obj.Add(GetProductsFromReader(dr));
                }
                dr.Close();
            }
            return obj;
        }


        public List<Products> GetTop6Product(String InClassKey, ref string error)
        {
            
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetTop6Product",
                CommandType.StoredProcedure, new SqlParameter("@inclasskey", InClassKey));
            return GetProductsCollectionFromReader(dr);
            
        }

        public List<Products> GetTop6ProductByCommod(String CommodKey, ref string error)
        {

            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetTop6ProductByCommodkey",
                CommandType.StoredProcedure, new SqlParameter("@Commodkey", CommodKey));
            return GetProductsCollectionFromReader(dr);

        }
        public Products GetProductsByItemKey(String ItemKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetProductByItemKey",
            CommandType.StoredProcedure, new SqlParameter("@itemkey", ItemKey));
            return (dr.Read() ? GetProductsFromReader(dr) : null);            
        }

        //Get All Products
        public List<Products> GetAllProducts(int PageIndex, int PageSize, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetAllProducts",
            CommandType.StoredProcedure, new SqlParameter("@PageIndex", PageIndex), new SqlParameter("@PageSize", PageSize));
            return GetProductsCollectionFromReader(dr);
            
        }

        // Get Count All records of Table  Products
        public int GetCountProducts(ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return int.Parse(obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCountProducts", CommandType.StoredProcedure));                   
            
        }

        // Get ProductsByInClassKey
        public List<Products> GetProductsByInClassKey(string InClassKey, int PageIndex, int PageSize, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetProductsByInClassKey",
            CommandType.StoredProcedure, new SqlParameter("@InClassKey", InClassKey), new SqlParameter("@PageIndex", PageIndex), new SqlParameter("@PageSize", PageSize));
            
            return GetProductsCollectionFromReader(dr);
            
        }

        public List<Products> GetProductsByCommodKey(string CommodKey, int PageIndex, int PageSize, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            SqlDataReader dr = obj.Get_Data_DataReader(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetProductsByCommodKey",
            CommandType.StoredProcedure, new SqlParameter("@CommodKey", CommodKey), new SqlParameter("@PageIndex", PageIndex), new SqlParameter("@PageSize", PageSize));

            return GetProductsCollectionFromReader(dr);

        }

        // Get ProductsCountByInClassKey
        public int GetProductsCountByInClassKey(string InClassKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return int.Parse(obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCountProductByInClassKey", CommandType.StoredProcedure, new SqlParameter("@InClassKey", InClassKey))); 
        }

        // Get ProductsCountByInClassKey
        public int GetProductsCountByCommodKey(string CommodKey, ref string error)
        {
            ClassExecute.ClassExecute obj = new ClassExecute.ClassExecute();
            return int.Parse(obj.ReturnValueFirstColumnOfFirstRow(Configuration.CurrentConnectionString, ref error, "TTr_Products_GetCountProductByCommodKey", CommandType.StoredProcedure, new SqlParameter("@CommodKey",CommodKey)));
        }
    }
}
